package com.authorization.demo.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.authorization.demo.enums.TokenExceptionEnum;
import com.authorization.demo.exception.TokenException;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Remarks:
 * @ClassName: TokenUtils
 * @Author: Micora
 * @CreateTime: 2022/7/26
 */
public class TokenUtils {
    //设置过期时间
    private static final long EXPIRE_DATE=1000*60*30; //30分钟
    //token秘钥
    private static final String TOKEN_SECRET = "ZCfasfhuaUUHufguGuwu2020BQWf";

    public static String token (String username, String password, List<String> userMenuList){

        String[] userMenus = userMenuList.toArray(new String[userMenuList.size()]);
        String token = "";
        try {
            //过期时间
            Date date = new Date(System.currentTimeMillis()+EXPIRE_DATE);
            //秘钥及加密算法
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            //设置头部信息
            Map<String,Object> header = new HashMap<>();
            header.put("typ","JWT");
            header.put("alg","HS256");
            //携带username，password信息，生成签名
            token = JWT.create()
                    .withHeader(header)
                    .withClaim("username",username)
                    .withClaim("password",password)
                    .withArrayClaim("userMenuList",userMenus)
                    .withExpiresAt(date)
                    .sign(algorithm);

        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }
        return token;
    }

    public static boolean verify(String token){
        /**
         * @desc   验证token，通过返回true
         * @params [token]需要校验的串
         **/
        try {
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            JWTVerifier verifier = JWT.require(algorithm).build();

            DecodedJWT jwt = verifier.verify(token);
            return true;
        }catch (Exception e){
            System.out.println("token校验失败");
            return  false;
        }
    }

    public static boolean powerCheck(String token,String powerName) {
        try {
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            JWTVerifier verifier = JWT.require(algorithm).build();

            DecodedJWT jwt = verifier.verify(token);
            String[] userMenus = jwt.getClaim("userMenuList").asArray(String.class);
            for (String menuName : userMenus) {
                if (menuName.equals(powerName)) {
                    return true;
                }
            }
        }catch (Exception e){
            System.out.println("登录过期");
            throw new TokenException(TokenExceptionEnum.ERROR_OVERDUE);
        }
        System.out.println("权限不足");
        throw new TokenException(TokenExceptionEnum.ERROR_NOT_HAVE_POWER);
    }
}
